package com.httpDemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.httpDemo.doMain.ResultObject;
import com.httpDemo.doMain.User;
import com.httpDemo.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * swagger测试！
 * 如果无法访问http://localhost:8081/springBoot/swagger-ui.html，可能是MyWebConfig.addResourceHandlers这里
 * 注册静态资源地址时swagger静态资源被拦截了。
 * 
 * @author Administrator
 *
 */
@Api(value = "用户管理", tags = { "用户管理tags" }) // api模块描述
@ApiResponses({ // 响应状态描述
		@ApiResponse(code = 401, message = "未授权"), @ApiResponse(code = 404, message = "不存在"),
		@ApiResponse(code = 500, message = "响应失败"),
		@ApiResponse(code = 200, message = "响应成功", response = ResultObject.class) })
@RestController
@RequestMapping(value = "user", produces = "application/json;charset=utf-8")
public class UserController {

	@Autowired
	private UserService userService;

	@ApiOperation(value = "分页查询接口", notes = "分页查询接口notes") // 接口描述
	@ApiImplicitParams({ // 接口参数描述
			@ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "query", dataType = "int") })
	@GetMapping(value = "findAll")
	@ResponseBody
	public PageInfo<User> findAll(int pageNum, int pageSize) {
		return userService.findAll(pageNum, pageSize);
	}

	@ApiOperation(value = "查询用户", notes = "查询用户notes")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "query", dataType = "int") })
	@GetMapping(value = "findById")
	@ResponseBody
	public ResultObject findById(Integer id) {
		User user = userService.findById(id);
		return new ResultObject(HttpStatus.OK.value(), user);
	}

	@ApiOperation(value = "新增用户", notes = "新增用户notes")
	@PostMapping(value = "save")
	@ResponseBody
	public ResultObject save(@RequestBody User user) {
		User count = userService.save(user);
		return new ResultObject(HttpStatus.OK.value(), count);
	}

	@ApiOperation(value = "修改用户", notes = "修改用户notes")
	@PostMapping(value = "update")
	@ResponseBody
	public ResultObject update(@RequestBody User user) {
		User count = userService.update(user);
		return new ResultObject(HttpStatus.OK.value(), count);
	}

	@ApiOperation(value = "删除用户", notes = "删除用户notes")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "query", dataType = "int") })
	@PostMapping(value = "delete")
	@ResponseBody
	public ResultObject delete(Integer id) {
		int count = userService.deleteById(id);
		return new ResultObject(HttpStatus.OK.value(), count);
	}
}
